import time
import asyncio
import requests
from bs4 import BeautifulSoup
import uuid
import schedule
from utils.HBaseConnect import HBaseConnect
from utils.MysqlConnect import MysqlConnect

url = 'https://top.baidu.com/board?tab=realtime'
tableName = "hot_search_keywords"


def save_hot_search_keywords(hbase, mysql, dic):
    print("排名" + dic['rank'], dic)
    hbase.putTable(tableName, rowKey=str(uuid.uuid1().hex), data={
        "info:keyword": dic['keyword'],
        "info:num": dic['searchNum'],
        "info:rank": dic['rank'],
        "info:url": dic['url'],
        "info:platform": "百度",
        "info:time": dic['time'],
    })
    insert_sql = (
        "INSERT INTO information(hot_keyword, search_num, ranking, url, platform,time) "
        "VALUES(%s, %s, %s, %s, %s, %s)")
    insert_args = (dic['keyword'], dic['searchNum'], dic['rank'], dic['url'],"百度",dic['time'])
    mysql.insert(insert_sql, insert_args)


async def get_hot_search_keywords():
    try:
        # 初始化hbase的参数
        hbase = HBaseConnect()
        hbase.start()
        mysql = MysqlConnect()
        current_time = time.strftime('%Y-%m-%d %H:%M:%S')
        res = requests.get(url)
        soup = BeautifulSoup(res.text, 'html.parser')
        items = soup.find_all('div', {'class': 'category-wrap_iQLoo'})
        rank = 0
        dic = {"time": current_time}
        for item in items:
            dic['url'] = item.find('a', {'class': 'look-more_3oNWC'}).get('href')
            dic['rank'] = str(rank)
            keyword = item.find('div', {'class': 'c-single-text-ellipsis'}).text
            dic['keyword'] = ' '.join(keyword.split())
            searchNum = item.find('div', {'class': 'hot-index_1Bl1a'}).text
            dic['searchNum'] = ' '.join(searchNum.split())
            save_hot_search_keywords(hbase, mysql, dic)
            rank += 1
        hbase.stop()
        # 关闭mysql
        mysql.cur.close()
        mysql.connection.close()
    except Exception as e:
        print("Exception", e)


def run_asyncio_task():
    print("当前时间", time.strftime('%Y-%m-%d %H:%M:%S'))
    asyncio.run(get_hot_search_keywords())


if __name__ == '__main__':
    # 在每小时的00, 10, 20, 30, 40, 50分钟运行任务
    schedule.every().hour.at(":00").do(run_asyncio_task)
    schedule.every().hour.at(":10").do(run_asyncio_task)
    schedule.every().hour.at(":20").do(run_asyncio_task)
    schedule.every().hour.at(":30").do(run_asyncio_task)
    schedule.every().hour.at(":40").do(run_asyncio_task)
    schedule.every().hour.at(":50").do(run_asyncio_task)
    while True:
        schedule.run_pending()
        time.sleep(1)
